package com.arthur.dao;

import com.arthur.bean.Cart;
import com.arthur.bean.Order;
import com.arthur.bean.Product;
import com.arthur.bean.User;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @所在项目:maven_ssm202008_wanghongzhao
 * @所在包:com.arthur.service
 * @作者:arthur_wang
 * @创建时间:2020-08-28 19:34:52
 * @说明:用户模块的业务实现
 **/
@Repository("userDao")
public class UserDaoImpl implements UserDao {
    @Autowired
    @Qualifier("sqlSessionTemplate")
    private SqlSessionTemplate sqlSessionTemplate;

    @Override
    public int loginByUsernameAndUserpwd(String username, String userpwd) throws Exception {
        int rows = 0;
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("username", username);
        paramMap.put("userpwd", userpwd);

        String statement = "com.arthur.mybatis.mapper.user" + "." + "loginByUsernameAndUserpwd";
        rows = sqlSessionTemplate.selectOne(statement, paramMap);
        return rows;
    }

    @Override
    public int deleteUserinfoById(int id) throws Exception {
        return sqlSessionTemplate.delete("com.arthur.mybatis.mapper.user.deleteUserinfoById", id);
    }

    @Override
    public int deleteCartinfoById(int id) throws Exception {
        return sqlSessionTemplate.delete("com.arthur.mybatis.mapper.user.deleteCartinfoById", id);
    }

    @Override
    public int obtainUserinfoRows() throws Exception {
        return sqlSessionTemplate.selectOne("com.arthur.mybatis.mapper.user.obtainUserinfoRows");
    }

    @Override
    public int registerByUserinfo(User user) throws Exception {
        return sqlSessionTemplate.insert("com.arthur.mybatis.mapper.user.registerByUserinfo", user);
    }

    @Override
    public int updateUser(User user) throws Exception {
        return sqlSessionTemplate.update("com.arthur.mybatis.mapper.user.updateUser", user);
    }

    @Override
    public List<User> fetchUserinfoListByStartindexAndRows(int startIndex, int rowsPerpage) throws Exception {
        Map<String, Integer> objectMap = new HashMap<>();
        objectMap.put("startIndex", startIndex);
        objectMap.put("rowsPerpage", rowsPerpage);
        return sqlSessionTemplate.selectList("com.arthur.mybatis.mapper.user.fetchUserinfoListByStartindexAndRows", objectMap);
    }

    @Override
    public User fetchUserinfoById(int id) throws Exception {
        return sqlSessionTemplate.selectOne("com.arthur.mybatis.mapper.user.fetchUserinfoById", id);
    }

    @Override
    public User fetchUserinfoByName(String name) throws Exception {
        return sqlSessionTemplate.selectOne("com.arthur.mybatis.mapper.user.fetchUserinfoByName",name);
    }

    @Override
    public Product fetchProductinfoByName(String name) throws Exception {
        return sqlSessionTemplate.selectOne("com.arthur.mybatis.mapper.user.fetchProductinfoByName",name);
    }

    @Override
    public int addOrderinfo(Order order) throws Exception {
        return sqlSessionTemplate.insert("com.arthur.mybatis.mapper.user.addOrderinfo", order);
    }

    @Override
    public int addCartinfo(Cart cart) throws Exception {
        return sqlSessionTemplate.insert("com.arthur.mybatis.mapper.user.addCartinfo", cart);
    }

    @Override
    public int obtainOrderinfoRows() throws Exception {
        return sqlSessionTemplate.selectOne("com.arthur.mybatis.mapper.user.obtainOrderinfoRows");
    }

    @Override
    public List<Order> fetchOrderinfoListByStartindexAndRows(int startIndex, int rowsPerpage,String username) throws Exception {
        Map<String,Object> objectMap = new HashMap<>();
        objectMap.put("startIndex", startIndex);
        objectMap.put("rowsPerpage", rowsPerpage);
        objectMap.put("username",username);
        return sqlSessionTemplate.selectList("com.arthur.mybatis.mapper.user.fetchOrderinfoListByStartindexAndRows", objectMap);
    }

    @Override
    public int obtainCartinfoRows() throws Exception {
        return sqlSessionTemplate.selectOne("com.arthur.mybatis.mapper.user.obtainCartinfoRows");
    }

    @Override
    public List<Cart> fetchCartinfoListByStartindexAndRows(int startIndex, int rowsPerpage, String username) throws Exception {
        Map<String,Object> objectMap = new HashMap<>();
        objectMap.put("startIndex", startIndex);
        objectMap.put("rowsPerpage", rowsPerpage);
        objectMap.put("username",username);
        return sqlSessionTemplate.selectList("com.arthur.mybatis.mapper.user.fetchCartinfoListByStartindexAndRows", objectMap);
    }

    @Override
    public Cart fetchCartinfoByid(int ioocid) throws Exception {
        return sqlSessionTemplate.selectOne("com.arthur.mybatis.mapper.user.fetchCartinfoByid", ioocid);
    }
}